FROM alpine:latest

# This target can be called in the following ways:
# 1. $ earthly --secret MY_SECRET=foobar +secrets
# 2. $ export MY_SECRET=foobar
#    $ earthly --secret MY_SECRET +secrets
# 3. $ export EARTHLY_SECRETS="MY_SECRET=foobar"
#    $ earthly +secrets
# 4. $ echo "MY_SECRET=foobar" > .env
#    $ earthly +secrets
secrets:
  ARG EARTHLY_SECRET_PATH
  RUN --secret MY_SECRET_VAR=+secrets/MY_SECRET test -n "$MY_SECRET_VAR"
  # the secret path can be omitted if the ID and the environment variable are matching
  RUN --secret MY_SECRET test -n "$MY_SECRET"
  RUN --mount=type=secret,id=+secrets/MY_SECRET,target=/root/secret ls /root/secret

  # Cloud Secrets (https://docs.earthly.dev/docs/guides/cloud-secrets)
  # Point secret values to cloud secrets. You can list secrets with:
  #  $ earthly secrets ls
  # RUN --secret MY_SECRET=+secrets/<cloud-secret-path> test -n "$MY_SECRET"
  # Render cloud secret paths dynamically using build args
  RUN --secret MY_SECRET=$EARTHLY_SECRET_PATH test -n "$MY_SECRET"
